package com.zl.tcbnewcar.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zl.tcbnewcar.R;
import com.zl.tcbnewcar.base.BaseActivity;
import com.zl.tcbnewcar.model.CarAdvertEntity;
import com.zl.tcbnewcar.utils.API;
import com.zl.tcbnewcar.utils.HttpUtils;
import com.zl.tcbnewcar.utils.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Request;

/**
 * @author zhanglei
 * @date 18/3/30
 * 广告页
 */
public class ADActivity extends BaseActivity {
    @BindView(R.id.launch_tv_index)
    TextView launchTvIndex;
    @BindView(R.id.iv_ad)
    ImageView ivAd;
    private CountDownTimer countDownTimer;
    private CarAdvertEntity carAdvertEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_ad);
        ButterKnife.bind(this);
        initView();
        initData();

    }

    private void initView() {
        //进入首页
        countDownTimer = new CountDownTimer(5 * 1000, 1000) {
            @Override
            public void onTick(long l) {
                launchTvIndex.setText(l / 1000 + "s跳过");
            }

            @Override
            public void onFinish() {
                //进入首页
                startActivity(new Intent(mActivity, MainActivity.class));
            }
        }.start();
    }

    private void initData() {
        HttpUtils.getInstance().GET(mActivity, API.getWelcomAdvert, new HttpUtils.OnOkHttpCallback() {
            @Override
            public void onSuccess(String body) {
                try {
                    JSONObject object = new JSONObject(body);
                    if (!object.optBoolean("result")) {
                        toMain();
                        return;
                    }
                    carAdvertEntity = new Gson().fromJson(object.optString("data"), CarAdvertEntity.class);
                    ImageLoader.loadImageUrl(mActivity, carAdvertEntity.getAdvertImg(), ivAd);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Request error, Exception e) {

            }
        });
    }

    @OnClick({R.id.iv_ad, R.id.launch_tv_index})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_ad:
                if (carAdvertEntity == null) return;
                if (TextUtils.isEmpty(carAdvertEntity.getAdvertHtml())) return;
                Intent intent = new Intent(mActivity, WebActivity.class);
                intent.putExtra("url", carAdvertEntity.getAdvertHtml());
                intent.putExtra("title", carAdvertEntity.getAdvertName());
                intent.putExtra("name", "ADActivity");
                startActivity(intent);
                countDownTimer.cancel();
                finish();
                break;
            case R.id.launch_tv_index:
                toMain();
                break;
        }
    }

    /**
     * 进入首页
     */
    private void toMain() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        //进入首页
        startActivity(new Intent(mActivity, MainActivity.class));
    }
}
